package cn.eoe.usinglistview;

import android.os.Bundle;
import android.support.v7.app.ActionBarActivity;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

/**
 * Created by lyb on 14-12-13.
 */
public class CustomListView extends ActionBarActivity {

    private ListView lv;
    @Override
    public void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.cutom_listview);

        lv = (ListView) findViewById(R.id.lv);
        lv.setAdapter(adapter);

    }

    private BaseAdapter adapter = new BaseAdapter() {

//        private String[] data = new String[]{"eoe1","eoe2","eoe3","eoe4","eoe5","eoe6","eoe7","eoe8","eoe9","eoe10","eoe11","eoe11","eoe12","eoe13","eoe14","eoe15"};
       private CustomListCellData[] data = new CustomListCellData[]{
            new CustomListCellData("img1","dec img1",R.drawable.img1),
            new CustomListCellData("img2","dec img2",R.drawable.img2),
            new CustomListCellData("img3","dec img3",R.drawable.img3),
        };

        @Override
        public int getCount() {
            return data.length;
        }

        @Override
        public CustomListCellData getItem(int position) {
            return data[position];
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(int position, View convertView, ViewGroup parent) {

//            System.out.println(">>>>>>>>>>>>>>>>");
//            TextView tv=null;
//            if(convertView!=null){
//                tv=(TextView) convertView;
//            }else if (tv==null){
//                tv = new TextView(CustomListView.this);
//            }
//
//            tv.setTextSize(50);
//            tv.setText((CharSequence) getItem(position));

//            return tv;
            LinearLayout ll =null;
            if (convertView!=null){
                ll = (LinearLayout) convertView;
            }else {
                ll = (LinearLayout) LayoutInflater.from(CustomListView.this).inflate(R.layout.custon_listcell,null);
            }

            CustomListCellData data = getItem(position);

            ImageView icon = (ImageView) ll.findViewById(R.id.icon);
            TextView name = (TextView) ll.findViewById(R.id.name);
            TextView dec = (TextView) ll.findViewById(R.id.dec);

            icon.setImageResource(data.iconId);
            name.setText(data.name);
            dec.setText(data.dec);

            return ll;

        }
    };
}
